package com.jinzecloud.blog.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinzecloud.blog.bean.User;
import com.jinzecloud.blog.constant.UserEnum;
import com.jinzecloud.blog.mapper.UserMapper;
import com.jinzecloud.blog.service.UserService;
import com.jinzecloud.blog.utils.Page;
import com.jinzecloud.blog.utils.RedisUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    RedisUtil redisUtil;

    public void add(User user) {
        user.setUPassword(SecureUtil.md5(user.getUPassword()));
        userMapper.addUser(user);
    }

    @Override
    public void alter(User user) {
        user.setUPassword(SecureUtil.md5(user.getUPassword()));
        userMapper.alteUser(user);
    }

    @Override
    public void delete(Long[] id) {
        userMapper.deleteUser(id);
    }

    @Override
    public User byId(Long id) {
        return userMapper.byId(id);
    }

    @Override
    public Page<User> listUser(Map<String, Object> params) {
        int currPage = Integer.parseInt(String.valueOf(params.get("currPage")));
        int totalCount = userMapper.totalCount();
        int pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
        if (params.get("currPage") == null && "0".equals(params.get("currPage"))) {
            params.put("currPage", 1);
        } else {
            params.put("currPage", (pageSize * (currPage - 1)));
        }
        params.put("pageSize", pageSize);
        List<User> user = userMapper.listUser(params);
        if (params.get("keyword") != null && params.get("keyword") != "") {
            totalCount = user.size();
        }
        Page<User> page = new Page<User>(user, totalCount, pageSize, currPage);

        return page;
    }

    @Override
    public void updateStatus(User user) {
        userMapper.updateStatus(user);
    }

    @Override
    public void updatePassword(User user) {
        user.setUPassword(SecureUtil.md5(user.getUPassword()));
        userMapper.updatePassword(user);
    }

    @Override
    public Map login(User user) {
        String prefix = "blog:user:";
        Map<String, Object> map = new HashMap<String, Object>();
        user.setUPassword(SecureUtil.md5(user.getUPassword()));
        User userLogin = userMapper.login(user);
        if (checkName(user.getUUsername()) == false){
            map.put("status",UserEnum.unregistered.getStatus());
            map.put("msg", UserEnum.unregistered.getMsg());
            return map;
        }
        if (userLogin == null) {
            map.put("status",UserEnum.passwordwrong.getStatus());
            map.put("msg", UserEnum.passwordwrong.getMsg());
            return map;
        }
        if (UserEnum.lockuser.getStatus().equals(userLogin.getUStatus())){
            map.put("status",UserEnum.lockuser.getStatus());
            map.put("msg", UserEnum.lockuser.getMsg());
            return map;
        }

        if (UserEnum.remove.getStatus().equals(userLogin.getUStatus())){
            map.put("status",UserEnum.remove.getStatus());
            map.put("msg", UserEnum.remove.getMsg());
            return map;
        }

        String uuid = UUID.randomUUID().toString();
        redisUtil.set( prefix + uuid, userLogin, 60L * 30L);
        map.put("status",UserEnum.succeed.getStatus());
        map.put("msg", UserEnum.succeed.getMsg());
        map.put("access_token",uuid);
        map.put("userId",userLogin.getUId());
        return map;
    }

    public boolean checkName(String name){
        int i = userMapper.checkName(name);
        if (i > 0) return true;
        return false;
    }
}
